import re


def ProcessMSToHHMMSS(milliseconds):
    seconds = milliseconds / 1000
    hours = int(seconds // 3600)
    seconds %= 3600
    minutes = int(seconds // 60)
    seconds %= 60
    return "%02d:%02d:%02d" % (hours, minutes, seconds)


def parse_bit_flags(bit_string, flag_descriptions):
    """将二进制字符串转换为描述字符串列表"""
    flags = []
    for idx, bit in enumerate(bit_string[::-1]):  # 逆序遍历
        if bit == '1':
            flags.append(flag_descriptions[idx])
    return flags


def convert_hex_to_voltage(hex_str):
    """将十六进制字符串转换为电压值"""
    return f"{round((int(hex_str, 16) * 1 * 3.3 * 11) / 4096, 2)}V ({int(hex_str, 16)})"


def format_system_time(hex_str):
    """将十六进制字符串转换为系统时间"""
    return f"{ProcessMSToHHMMSS(int(hex_str, 16))} ({int(hex_str, 16)}ms)"


def append_or_blank(old_values: list, new_value, obj_text, listData, filter=False):
    """如果new_value在old_values中，添加空格，否则添加new_value"""
    if new_value in old_values[0]:
        listData.append(None)
    else:
        listData.append(obj_text)
        if filter:
            old_values[0] = new_value


kAdcTempTab = [
    3896, 3885, 3873, 3862, 3849, 3836, 3823, 3809,
    3794, 3779, 3763, 3747, 3730, 3712, 3694, 3675,
    3656, 3636, 3615, 3594, 3572, 3549, 3526, 3502,
    3477, 3452, 3426, 3399, 3372, 3344, 3315, 3286,
    3256, 3225, 3194, 3162, 3130, 3097, 3063, 3029,
    2995, 2960, 2925, 2889, 2853, 2816, 2779, 2742,
    2704, 2667, 2629, 2590, 2552, 2513, 2474, 2435,
    2396, 2357, 2318, 2279, 2240, 2202, 2163, 2124,
    2086, 2048, 2009, 1971, 1934, 1896, 1859, 1822,
    1786, 1750, 1714, 1679, 1644, 1609, 1575, 1542,
    1509, 1476, 1444, 1412, 1381, 1350, 1319, 1290,
    1260, 1232, 1203, 1175, 1148, 1121, 1095, 1070,
    1044, 1020, 995, 972, 948, 926, 904, 883,
    862, 841, 821, 802, 783, 764, 746, 728,
    711, 694, 677, 661, 645, 630, 615, 600,
    586, 572, 558, 545, 532, 519, 495, 483,
    483, 472, 461, 450, 439, 429, 419, 409,
    399, 390, 381, 372, 363, 355, 347, 339,
    331, 324, 316, 309, 302, 295, 289, 282,
    276, 270, 264, 258, 252, 246, 241, 236,
    230, 225, 221, 216, 211, 207
]


def CtApPowerAdcToTemperature(adc_value):
    # 处理ADC值高于表格第一个元素的情况
    if adc_value >= kAdcTempTab[0]:
        return -40

    # 处理ADC值低于表格最后一个元素的情况
    last_index = len(kAdcTempTab) - 1
    if adc_value <= kAdcTempTab[last_index]:
        return last_index - 40

    # 二分查找实现
    left = 0
    right = last_index
    mid = 0

    while left < right:
        mid = (left + right) // 2  # Python整数除法

        # 找到精确匹配
        if adc_value == kAdcTempTab[mid]:
            break

        # 检查是否在两个相邻值之间
        if mid < last_index:
            if kAdcTempTab[mid] > adc_value > kAdcTempTab[mid + 1]:
                # 比较距离选择更接近的值
                delta_prev = kAdcTempTab[mid] - adc_value
                delta_next = adc_value - kAdcTempTab[mid + 1]
                mid = mid if delta_prev < delta_next else mid + 1
                break

        # 调整搜索边界
        if adc_value > kAdcTempTab[mid]:
            right = mid
        else:
            left = mid + 1  # 优化搜索逻辑

    return mid - 40


def convert_hex_to_temperature(hex_str):
    """将十六进制字符串转换为温度值"""
    return f"{CtApPowerAdcToTemperature(int(hex_str, 16))} °C ({hex_str})"


def extract_window_classes(file_path):
    # 读取文件内容
    with open(file_path, 'r') as file:
        file_content = file.read()

    # 正则表达式匹配带 Window 后缀的类名，仅从 import 语句中提取
    pattern = r'from\s+\.(\w+)\s+import\s+(\w*Window)\b'
    matches = re.findall(pattern, file_content)

    # 返回匹配到的模块名和类名
    return matches
